package org.catdroid.bcnappsjam.barcelonaparticipa.task.base;

import java.util.HashMap;
import java.util.Map;

import org.catdroid.bcnappsjam.barcelonaparticipa.util.HttpUtils;

import android.content.Context;

public class SendTask extends FewlapsAsyncTask<Void, Void, Exception>{

	private Context context = null;
	private FewlapsTaskLauncher launcher = null;
	private String resposta = null;

	private String result = null;

	private SendTask(Context context, FewlapsTaskLauncher launcher, String resposta) {
		this.context = context;
		this.launcher = launcher;
		this.resposta = resposta;
	}

	public static void call(Context context, FewlapsTaskLauncher launcher, String resposta) {
		SendTask task = new SendTask(context, launcher, resposta);
		task.executeOnExecutor(FewlapsAsyncTask.THREAD_POOL_EXECUTOR);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();

		running = true;
	}

	@Override
	protected Exception doInBackground(Void... v) {
		try {
			Map params = new HashMap();
			params.put("idUser", "1");
			params.put("idReply", resposta);
			result = HttpUtils.requestData("participar", params, HttpUtils.GET, context);
		} catch (Exception e) {
			return e;
		}
		return null;
	}

	@Override
	protected void onPostExecute(Exception e) {
		super.onPostExecute(e);

		running = false;
		if (e == null) {
			launcher.executeAsyncOk(result);
		} else {
			launcher.executeAsyncError(e);
		}
	}

	private static boolean running = false;

	public static boolean isRunning() {
		return running;
	}
}
